home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group99a.txt
/
000095_icon-group-sender _Fri Apr 9 08:17:45 1999.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
2KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id IAA12257
for icon-group-addresses; Fri, 9 Apr 1999 08:17:37 -0700 (MST)
Message-Id: <199904091517.IAA12257@baskerville.CS.Arizona.EDU>
From: swampler@noao.edu (Steve Wampler)
Date: Thu, 8 Apr 1999 18:37:14 MST
To: "Hudon, Christian (EXCHANGE:MTL:6X16)" <chudon@americasm01.nt.com>,
icon-group@optima.CS.Arizona.EDU
Subject: Re: Deep copy in Icon?
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
> Hi,
>
> could someone tell me what's wrong with the following snippet of Icon
> code?
>
> procedure deepcopy(x, t)
> /t := table()
> case type(x) of {
> "list"|"table"|"set"|"record" : {
> e := t[image(x)]
> if /e then {
> e := copy(x)
> t[image(x)] := e
> every sub := !e do sub := deepcopy(sub, t)
> }
> return e
> }
> default: return x
> }
> end
Hi Christian,
This isn't the problem, but you just use t[x] instead of t[image(x)]...
Where you're problem shows up is in the "every sub := !e do sub := ..."
line.
Immediately after each iteration of "sub := !e" you nicely have
the *value* of the variable sub the same value as in e, but...
The assignment of "sub := deepcopy(..." is replacing the value
of sub with a new value, but isn't touching the list e, where
the original value stays untouched. (i.e. before the assignment,
sub referenced the same value referenced in e, but now the
value of sub references elsewhere).
Here's one solution, perhaps someone has a cleaner one:
procedure deepcopy(e, t)
/t := table()
if type(e) == ("list"|"table"|"set"|"record") then {
e := copy(e)
every a := e[i := 1 to *e] do {
/t[a] := deepcopy(a, t)
e[i] := t[a]
}
}
return e
end